package com.woniu.bank.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.bank.dto.GroupDTO;
import com.woniu.bank.dto.GroupMemberDefaultDataDTO;
import com.woniu.bank.dto.NewGroupDTO;
import com.woniu.bank.service.GroupService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.GroupMemberDefaultDataVO;
import com.woniu.bank.vo.GroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 联保小组额度申请控制层
 *
 * @author 晨星
 * @date 2023/01/04/14:31
 */
@RestController
@RequestMapping("group")
@Api(tags = "联保小组额度申请接口")
public class GroupController {

    private final GroupService groupService;

    public GroupController(GroupService groupService) {
        this.groupService = groupService;
    }

    /**
     * 查询联保小组
     *
     * @author 晨星
     * @date 2023/1/4 16:28
     **/
    @PostMapping("conditionQuery")
    @ApiOperation(value = "查询联保小组", notes = "根据条件查询所有符合的联保小组额度申请。只有小组名称可模糊查询，其他都为精确查询，" +
            "证件类型只能为中国大陆身份证且要与证件号码进行联立查询", httpMethod = "POST")
    public Result conditionQuery(GroupDTO group) {
        List<GroupVO> groupList = groupService.conditionQuery(group);
        PageInfo<GroupVO> pageInfo = new PageInfo<>(groupList);
        return Result.ok().data("pageInfo", pageInfo);
    }

    @PostMapping("defaultData")
    @ApiOperation(value = "新增联保小组需要的默认数据", notes = "新增联保小组需要的默认数据", httpMethod = "POST")
    public Result defaultData(@RequestBody List<GroupMemberDefaultDataDTO> members) {
        GroupMemberDefaultDataVO defaultData = groupService.createDefaultData(members);
        return Result.ok().data("groupList", defaultData);
    }

    /**
     * 根据组长客户号，显示组长名称
     *
     * @author 晨星
     * @date 2023/1/5 16:18
     **/
    @PostMapping("selectLeaderName")
    @ApiOperation(value = "联保小组新增-根据组长客户号，显示组长名称", notes = "根据组长客户号，显示组长名称", httpMethod = "POST")
    public Result selectGroupLeaderName(String groupLeaderNo) {
        String groupLeaderName = groupService.selectGroupLeaderName(groupLeaderNo);
        return Result.ok().data("groupLeaderName", groupLeaderName);
    }

    /**
     * 联保小组额度申请即新增
     *
     * @author 晨星
     * @date 2023/1/6 11:42
     **/
    @PostMapping("add")
    @ApiOperation(value = "联保小组额度申请即新增", notes = "联保小组额度申请即新增", httpMethod = "POST")
    public Result add(@RequestBody NewGroupDTO group) {
        int i = groupService.add(group);
        if (i == 1) {
            return Result.ok("已提交至审批");
        } else {
            return Result.error("提交失败，请填写完整信息");
        }
    }

    /**
     * 退出新增小组时，删除小组编号
     *
     * @param groupMemberNoList 小组所有成员的客户号
     * @return com.woniu.bank.util.Result
     * @author 晨星
     * @date 2023/1/6 21:12
     **/
    @PostMapping("quit")
    @ApiOperation(value = "退出新增小组时，删除小组编号", notes = "退出新增小组时，删除小组编号", httpMethod = "POST")
    @ApiImplicitParam(name = "groupMemberNoList",value = "小组所有成员的客户号",dataType = "Array",required = true)
    public Result quit(@RequestBody List<String> groupMemberNoList) {
        groupService.quit(groupMemberNoList);
        return Result.ok();
    }

    /**
     * 联保小组软删除
     *
     * @author 晨星
     * @date 2023/1/6 11:42
     **/
    @PostMapping("isDeleted")
    @ApiOperation(value = "联保小组软删除", notes = "联保小组逻辑删除", httpMethod = "POST")
    public Result isDeleted(@RequestBody GroupVO group) {
        int i = groupService.isDeleted(group);
        if (i == 1) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败，请联系工作人员");
        }
    }

}
